package structure;

import java.util.List;

public class MyLinkedList implements IList {
    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()){
            throw new ListIndexOutOfBoundsException("链表的index位置不合法");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    public ListNode findIndex(int index){
        ListNode cur = head;
        while(index-- > 0){
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //删除头结点
                if(cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }//删除尾结点
                }else if(cur == last){
                    last = last.prev;
                    if(last != null) {
                        last.next = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //删除头结点
                if(cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }//删除尾结点
                }else if(cur == last){
                    last = last.prev;
                    if(last != null) {
                        last.next = null;
                    }
                }else{
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode node = head;
        while(node != null){
            len++;
            node = node.next;
        }
        return len;
    }

    @Override
    public void display() {
        System.out.print("[");
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val);
            if(cur.next != null){
                System.out.print(", ");
            }
            cur = cur.next;
        }
        System.out.print("]");
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curn = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curn;
        }
        head = null;
        last = null;
    }

    public static class ListIndexOutOfBoundsException extends RuntimeException{
        public ListIndexOutOfBoundsException(){

        }
        public ListIndexOutOfBoundsException(String message){
            super(message);
        }
    }
}
